package com.gcy.checkcertificate;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Objects;

/**
 * @author linmiaolai
 */
@SpringBootApplication
@Slf4j
public class CheckCertificateApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(CheckCertificateApplication.class, args);
        printHomeUrl(run);
    }

    /**
     * 输出主页信息
     */
    private static void printHomeUrl(ApplicationContext ctx) {
        String ip = localIP();
        String serverPort = ctx.getEnvironment().getProperty("server.port");
        String contextPath = ctx.getEnvironment().getProperty("server.servlet.context-path");
        if (StringUtils.isBlank(contextPath)) {
            contextPath = "/";
        } else if (!contextPath.startsWith("/")) {
            contextPath = "/" + contextPath;
        }
        if (Objects.equals(contextPath, "/")) {
            contextPath = "";
        }
        log.info("主页URL: http://{}:{}{}", ip, serverPort, contextPath);
        log.info("旧swagger页面URL: http://{}:{}{}/swagger-ui.html", ip, serverPort, contextPath);
        log.info("新swagger页面URL: http://{}:{}{}/doc.html", ip, serverPort, contextPath);
    }

    public static String localIP() {
        try {
            String ip = System.getProperty("com.alibaba.nacos.client.naming.local.ip",
                    InetAddress.getLocalHost().getHostAddress());
            return ip;
        } catch (UnknownHostException e) {
            return "resolve_failed";
        }
    }
}
